<template>
	<view class="container">
		<u-form :model="form" ref="uForm">
			<u-form-item label="岗位名称" prop="vacancyName">
				<u-input v-model="form.vacancyName" placeholder="请输入岗位名称" />
			</u-form-item>
			
			<u-form-item label="薪资待遇" prop="salary">
				<u-input v-model="form.salary" placeholder="请输入薪资待遇" />
			</u-form-item>
			
			<u-form-item label="工作地点" prop="address">
				<u-input v-model="form.address" placeholder="请输入工作地点" />
			</u-form-item>
			
			<u-form-item label="公司名称" prop="company">
				<u-input v-model="form.company" placeholder="请输入公司名称" />
			</u-form-item>
			
			<u-form-item label="公司规模" prop="companyNumber">
				<u-input v-model="form.companyNumber" placeholder="请输入公司规模" />
			</u-form-item>
			
			<u-form-item label="公司行业" prop="companyIndustry">
				<u-input v-model="form.companyIndustry" placeholder="请输入公司行业" />
			</u-form-item>
			
			<u-form-item label="公司简介" prop="companyCondition">
				<u-input v-model="form.companyCondition" type="textarea" placeholder="请输入公司简介" />
			</u-form-item>
			
			<u-form-item label="岗位详情" prop="detailed">
				<u-input v-model="form.detailed" type="textarea" placeholder="请输入岗位详情" />
			</u-form-item>
		</u-form>
		
		<view class="btn-group">
			<u-button type="primary" @click="submit">保存</u-button>
			<u-button type="default" @click="cancel">取消</u-button>
		</view>
	</view>
</template>

<script>
import { detail, save } from '../../utils/api/modules/vacancy';

export default {
	data() {
		return {
			id: '',
			form: {
				vacancyName: '',
				salary: '',
				address: '',
				company: '',
				companyNumber: '',
				companyIndustry: '',
				companyCondition: '',
				detailed: ''
			},
			rules: {
				vacancyName: [{
					required: true,
					message: '请输入岗位名称',
					trigger: ['blur', 'change']
				}],
				salary: [{
					required: true,
					message: '请输入薪资待遇',
					trigger: ['blur', 'change']
				}],
				address: [{
					required: true,
					message: '请输入工作地点',
					trigger: ['blur', 'change']
				}],
				company: [{
					required: true,
					message: '请输入公司名称',
					trigger: ['blur', 'change']
				}],
				companyNumber: [{
					required: true,
					message: '请输入公司规模',
					trigger: ['blur', 'change']
				}],
				companyIndustry: [{
					required: true,
					message: '请输入公司行业',
					trigger: ['blur', 'change']
				}],
				companyCondition: [{
					required: true,
					message: '请输入公司简介',
					trigger: ['blur', 'change']
				}],
				detailed: [{
					required: true,
					message: '请输入岗位详情',
					trigger: ['blur', 'change']
				}]
			}
		}
	},
	onLoad(options) {
		if (options.id) {
			this.id = options.id;
			this.loadData();
		}
	},
	methods: {
		async loadData() {
			try {
				const res = await detail({ id: this.id });
				this.form = res.data;
			} catch (error) {
				console.error('加载数据失败:', error);
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				});
			}
		},
		submit() {
			this.$refs.uForm.validate(valid => {
				if (valid) {
					this.saveData();
				}
			});
		},
		async saveData() {
			const user = uni.getStorageSync('user');
			if (!user || !user.id) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				return;
			}
			
			try {
				const data = {
					...this.form,
					posterId: user.id,
					posterName: user.nickName,
					posterPic: user.avatarUrl
				};
				
				if (this.id) {
					data.id = this.id;
				}
				
				await save(data);
				uni.showToast({
					title: '保存成功',
					icon: 'success'
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			} catch (error) {
				console.error('保存失败:', error);
				uni.showToast({
					title: '保存失败',
					icon: 'none'
				});
			}
		},
		cancel() {
			uni.navigateBack();
		}
	}
}
</script>

<style>
.container {
	padding: 20rpx;
}

.btn-group {
	margin-top: 40rpx;
	display: flex;
	justify-content: space-around;
}

.btn-group .u-button {
	width: 45%;
}
</style> 